package com.sion.difytest;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Message> data;
    private Context context;

    public MyAdapter(List<Message> data, Context context) {
        this.data = data;
        this.context = context;
    }



    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_layout, null);
        return new ViewHolder(view);
    }


    public void onBindViewHolder( MyAdapter.ViewHolder holder, int position) {
        Message message = data.get(position);
        if (message.getType() == Message.TYPE_RECEIVE) {
            holder.linear_left.setVisibility(View.VISIBLE);
            holder.linear_right.setVisibility(View.GONE);
            holder.left_text.setText(message.getContent());
        } else {
            holder.linear_left.setVisibility(View.GONE);
            holder.linear_right.setVisibility(View.VISIBLE);
            holder.right_text.setText(message.getContent());
        }

    }


    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout linear_left;
        private LinearLayout linear_right;
        private TextView left_text;
        private TextView right_text;

        public ViewHolder( View itemView) {

            super(itemView);
            linear_left = itemView.findViewById(R.id.linear_left);
            linear_right = itemView.findViewById(R.id.linear_right);
            left_text = itemView.findViewById(R.id.msg_left);
            right_text = itemView.findViewById(R.id.msg_right);
        }
    }
}